package cn.tecnova.utils

import java.sql.ResultSet
import java.text.SimpleDateFormat
import java.util.{Date, UUID}

import cn.tecnova.bean.{BaAnalysisBean, DiyRelationArticle, NlpJsonBean}
import com.google.gson.Gson
import org.apache.commons.lang3.StringUtils
import org.apache.spark.broadcast
import org.apache.spark.sql.Row

import scala.util.control.Breaks

/**
  * description:DiyRelationArticle工具类
  **/
object DiyRelationArticleUtils {



  /**
    * 判断内容是否包含排除词
    *
    * @param exclusionWords 排除词数组
    * @param content        内容是文章或是标题或是标题与文章
    */
  def contentIsExclusion(exclusionWords: Array[String], content: String) = {

    var exclusionList = List[Boolean]()
    for (exclusionWord: String <- exclusionWords) {
      //如果用户自定义内容包含排除词  这篇文章不要
      val f = content.contains(exclusionWord)
      exclusionList :+= f
    }
    //包含排除词
    val bool = if (exclusionList.contains(true)) false else true

    bool

  }


  /**
    * 判断是否包含关键词
    *
    * @param regionWords  地域关键词
    * @param subjectWords 主体关键词
    * @param eventWords   事件关键词
    * @param content      文章或标题内容
    * @return 是否包含
    */
  def isNotContainsWordsAndGetHit(regionWords: String, subjectWords: String, eventWords: String, content: String) = {

    val regionSplited = regionWords.split(",")
    val subjectSplited = subjectWords.split(",")
    val eventSplited = eventWords.split(",")

    var hitWords = ""
    val loop = new Breaks;

    //为了跳出循环
    loop.breakable {

      //遍历三三组合，判断是否同时满足
      for (region <- regionSplited) {
        for (subject <- subjectSplited) {
          for (event <- eventSplited) {
            //如果有一组包含
            if (content.contains(region) && content.contains(subject) && content.contains(event)) {

              hitWords = region + "," + subject + "," + event
              //跳出循环
              loop.break
            }
          }
        }
      }
    }
    hitWords
  }


  /**
    * 判断文章的"site_url":"站点网址" 是否包含用户自定义的包含网站内容contain_urls
    * @param containUrls 用户自定义的包含网站内容containUrls
    * @param siteUrl 文章的站点网址
    */
  def siteUrlContainsContainUrls(containUrls:String,siteUrl:String) ={

    //1表示有包含网址 0表示没有包含网址
    var containUrlsList = List[Int]()
    val containUrlsArr: Array[String] = containUrls.split("\\|")
    for(exclusionUrl <- containUrlsArr){
      //如果文章站点网址包含用户自定义网址
      if(siteUrl.contains(exclusionUrl)){
        containUrlsList :+= 1
      }
    }

    //用户定义的包含网址是否有文章的站点网址？
    containUrlsList.contains(1)

  }

  /**
    * 判断文章的"site_url":"站点网址" 是否包含用户自定义的禁止网站内容exclusion_urls
    * @param exclusionUrls 用户自定义的禁止网站内容exclusionUrls
    * @param siteUrl 文章的站点网址
    */
  def siteUrlContainsExclusionUrls(exclusionUrls:String,siteUrl:String) ={

    //1表示有禁止网址 0表示没有
    var exclusionUrlsList = List[Int]()
    val exclusionUrlsArr: Array[String] = exclusionUrls.split("\\|")
    for(exclusionUrl <- exclusionUrlsArr){
      //如果文章站点网址包含用户自定义网址
      if(siteUrl.contains(exclusionUrl)){
        exclusionUrlsList :+= 1
      }
    }

    //用户定义的禁止网址是否有文章的站点网址？
    exclusionUrlsList.contains(1)

  }


  /**
    *
    * @param js
    * @param mediaDic
    * @param mediaCls
    * @param userId
    * @param groupId
    * @param groupName
    * @param groupLineId
    * @param regionWords
    * @param subjectWords
    * @param eventWords
    * @param exclusionWords
    * @param content
    * @return
    */
  def getValues(js:NlpJsonBean,mediaDic: broadcast.Broadcast[Map[String, String]], mediaCls: String, userId: String, groupId: String, groupName: String, groupLineId: String, regionWords: String, subjectWords: String, eventWords: String, exclusionWords: String, content: String) = {

    var value = ""

    //如果用户定义了排除词 就去判断
    if(StringUtils.isNotEmpty(exclusionWords)){

      val exclusionWordsArr = exclusionWords.split(",")

      //判断内容是否包含排除词
      val flag: Boolean = DiyRelationArticleUtils.contentIsExclusion(exclusionWordsArr, content)

      //如果不包含排除词
      if (flag) {

        //自定义词组命中的词
        val hitWords: String = DiyRelationArticleUtils.isNotContainsWordsAndGetHit(regionWords, subjectWords, eventWords, content)
        //包含一个组合
        if (StringUtils.isNotEmpty(hitWords)) {

          //将匹配到的关键词加上颜色标签
          var articleContent = js.article_content
          val hitWordsArr: Array[String] = hitWords.split(",")

          for(word <- hitWordsArr){

            articleContent = articleContent.replaceAll(word,"<span class=\"weight\">"+word+"</span>")

          }

          //文章按条件筛选完成
          val id = UUID.randomUUID().toString.replaceAll("-", "")
          val update_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date)

          val gson = new Gson()
          val article: BaAnalysisBean = BaAnalysisBean(id,js.site_id,js.template_source,js.uuid,js.site_name,js.site_url,js.domain,js.weight,js.main_id,js.site_cls,js.enterprise_scale_cls,js.customer_cls,mediaDic.value.getOrElse(mediaCls, mediaCls),js.content_cls,js.important_leavel,js.industry_cls,js.language_cls,js.area_cls,js.site_province,js.site_city,js.site_district,js.schedule,js.article_url,js.code,js.article_html,js.article_channel,js.article_title,js.article_author,js.article_source,js.article_pubdate,js.article_view,js.article_reply,articleContent,js.article_like,js.article_forword,js.gmt_create,js.nlp_emotion," "," "," "," "," ",hitWords," ",userId," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ",groupId,groupName,groupLineId," ",update_time," "," "," "," "," "," ","ba_diy_risk_relation_article")
          value = gson.toJson(article)

        }
      }

    }else{
      //自定义词组命中的词
      val hitWords: String = DiyRelationArticleUtils.isNotContainsWordsAndGetHit(regionWords, subjectWords, eventWords, content)
      //包含一个组合
      if (StringUtils.isNotEmpty(hitWords)) {

        //将匹配到的关键词加上颜色标签
        var articleContent = js.article_content
        val hitWordsArr: Array[String] = hitWords.split(",")

        for(word <- hitWordsArr){

          articleContent = articleContent.replaceAll(word,"<span class=\"weight\">"+word+"</span>")

        }

        //文章按条件筛选完成
        val id = UUID.randomUUID().toString.replaceAll("-", "")
        val update_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date)

        val gson = new Gson()
        val article: BaAnalysisBean = BaAnalysisBean(id, js.site_id, js.template_source, js.uuid, js.site_name, js.site_url, js.domain, js.weight, js.main_id, js.site_cls, js.enterprise_scale_cls, js.customer_cls, mediaDic.value.getOrElse(mediaCls, mediaCls), js.content_cls, js.important_leavel, js.industry_cls, js.language_cls, js.area_cls, js.site_province, js.site_city, js.site_district, js.schedule, js.article_url, js.code, js.article_html, js.article_channel, js.article_title, js.article_author, js.article_source, js.article_pubdate, js.article_view, js.article_reply, articleContent, js.article_like, js.article_forword, js.gmt_create, js.nlp_emotion, " ", " ", " ", " ", " ", hitWords, " ", userId, " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", groupId, groupName, groupLineId, " ", update_time, " ", " ", " ", " ", " ", " ", "ba_diy_risk_relation_article")
        value = gson.toJson(article)
      }
    }

    value
  }


}
